package com.shpng.cheapenglish.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shpng.cheapenglish.entity.SUser;
import com.shpng.cheapenglish.manager.UserManager;

public class LoginAction {

	public String execute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		UserManager userManager = UserManager.getInstance();
		String email = req.getParameter("email");
		email = (email == null ? "" : email.trim());
		if ("".equals(email)) {
			req.setAttribute("error", "Email is empty!");
			req.getRequestDispatcher("/pages/welcome.jsp");
		}
		SUser user = userManager.getUser(email);
		if (user != null) {
			req.getSession().setAttribute("email", email);
//			req.getSession().setAttribute("userId", user.getId());
			return "/pages/word-input.jsp";

		} else {
			req.setAttribute("error", "User doesn't exist!");
			return "/pages/welcome.jsp";

		}

	}

}
